package com.dingshi.security;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dingshi.security.adapter.TrackAdapter;
import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.RepairApi;
import com.dingshi.security.db.SettingLoader;
import com.dingshi.security.entity.Track;
import com.dingshi.security.util.Utils;
import com.dingshi.security.view.LoadDialog;

/**
 *  上报1
	派工1
	接报2
	维修3
	维修完毕4
	确认完毕5.

 * @author Administrator
 *
 */
public class RepairTrackListActivity extends BaseActivity implements OnClickListener{
	protected static final int ID_LOADING = 2300;
	private Button btn_back;
	private TextView title_text;
	private Button btn_right;
	private TextView btn_report;
	private TextView btn_receive;
	private TextView btn_repair;
	private ListView listView;
	protected ArrayList<Track> list;
	private TrackAdapter adapter;
	private TextView tips;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repair_track);
		
		initTitle();
		initComponents();
		registerButton();
		
		listView.setCacheColorHint(Color.TRANSPARENT);
		listView.setDividerHeight(0);
		
		getTrackList(SettingLoader.getGuardNum(this), SettingLoader.getLoginUserType(this));
		
		btn_report.setTextColor(getResources().getColor(R.color.white));//1
		btn_receive.setTextColor(getResources().getColor(R.color.black));//2
		btn_repair.setTextColor(getResources().getColor(R.color.black));//3
	}
	
	private void getTrackList(String guardNum, String deprNum) {

		RepairApi api = new RepairApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
				JSONObject jsonEntity = apiResult.getJsonEntity();
				Log.e("aa", jsonEntity.toString());
				if (jsonEntity != null) {
					// {"d":[{"__type":"RepairBillListReturn:r","d":"4","t":"2013-3-28 10:03:55","e":"双兴摄像头4","s":"5","tc":"","a":"双兴小区","n":"张三"},
					//{"__type":"RepairBillListReturn:r","d":"5","t":"2013-3-22 13:48:56","e":"双兴摄像头5","s":"5","tc":"","a":"双兴小区","n":"张三"},
					//{"__type":"RepairBillListReturn:r","d":"6","t":"2013-3-22 13:52:45","e":"双兴球机1","s":"5","tc":"","a":"双兴小区","n":"张三"},
					//{"__type":"RepairBillListReturn:r","d":"7","t":"2013-3-22 13:59:33","e":"双兴球机2","s":"2","tc":"","a":"乐园小区","n":"张三"},
					//{"__type":"RepairBillListReturn:r","d":"8","t":"2013-3-28 10:03:55","e":"双兴球机3","s":"5","tc":"","a":"乐园小区","n":"张三"}],
					//"t":1}
					


					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							break;
						case 1:// 数组
							JSONArray array = jsonEntity.getJSONArray("d");
							if (array != null) {
								list = new ArrayList<Track>();
								Track track = null;
								Log.e("array", "array is " + array.length());
								for (int i = 0; i < array.length(); i++) {
									track = new Track();
									JSONObject opt = (JSONObject) array.opt(i);
									track.setOddNum(opt.getString("d"));
									track.setArea(opt.getString("a"));
									track.setName(opt.getString("n"));
									track.setStatus(opt.getString("s"));
									Log.e("status", "status is " + opt.getString("s"));
									track.setReportDate(opt.getString("t"));
									if(opt.has("tc")) {
										track.setFinishDate(opt.getString("tc"));
									}
									track.setDeviceName(opt.getString("e"));
									list.add(track);
								}
								Log.i("Track", "size is " + list.size());
							}

							updateList("1");

							break;
						case -1:// 错误
							dismissDialog(ID_LOADING);
							updateList("1");
							break;

						default:
							dismissDialog(ID_LOADING);
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
				updateList("1");
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(ID_LOADING);
			}
		});
		api.getRepairList(1, SettingLoader.getGuardNum(this), SettingLoader.getLoginUserType(this));

	}

	private void updateList(String type) {
		ArrayList<Track> mList = handle(list,type);
		if (mList != null && mList.size() > 0) {
			tips.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
			Log.e("list", "diary count is " + mList.size());
			adapter = new TrackAdapter(RepairTrackListActivity.this, mList);
			listView.setAdapter(adapter);
		} else {
			tips.setVisibility(View.VISIBLE);
			listView.setVisibility(View.GONE);
		}
	}
	
	
	private ArrayList<Track> handle(ArrayList<Track> list,String type) {
		ArrayList<Track> mList = new ArrayList<Track>();
		for(Track track : list) {
			Log.e("status", "status is " + track.getStatus());
			if(type.equals(track.getStatus())) {
				mList.add(track);
			}
		}
		return mList;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ID_LOADING:
			LoadDialog dialog = new LoadDialog(this, getResources().getString(R.string.login_loading));
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}
	
	private void initTitle() {
		btn_back = (Button)findViewById(R.id.btn_back);
		btn_right = (Button)findViewById(R.id.btn_refresh);
		btn_right.setVisibility(View.VISIBLE);
		title_text = (TextView)findViewById(R.id.title_text);
		title_text.setText("报修单跟踪");
		
	}

	private void registerButton() {
		btn_back.setOnClickListener(this);
		btn_report.setOnClickListener(this);
		btn_receive.setOnClickListener(this);
		btn_repair.setOnClickListener(this);
		btn_right.setOnClickListener(this);
	}

	private void initComponents() {
		btn_report = (TextView)findViewById(R.id.textView1);
		btn_receive = (TextView)findViewById(R.id.textView2);
		btn_repair = (TextView)findViewById(R.id.textView3);
		//
		tips = (TextView)findViewById(R.id.tips);
		listView = (ListView)findViewById(R.id.listView1);
		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.textView1:
			Log.e("Click", "btn_report");
			btn_report.setTextColor(getResources().getColor(R.color.white));
			btn_receive.setTextColor(getResources().getColor(R.color.black));
			btn_repair.setTextColor(getResources().getColor(R.color.black));
			updateList("1");
			break;
		case R.id.textView2:
			btn_report.setTextColor(getResources().getColor(R.color.black));
			btn_receive.setTextColor(getResources().getColor(R.color.white));
			btn_repair.setTextColor(getResources().getColor(R.color.black));
			updateList("3");
			break;
		case R.id.textView3:
			btn_report.setTextColor(getResources().getColor(R.color.black));
			btn_receive.setTextColor(getResources().getColor(R.color.black));
			btn_repair.setTextColor(getResources().getColor(R.color.white));
			updateList("4");
			break;
		case R.id.btn_refresh:
			btn_report.setTextColor(getResources().getColor(R.color.white));//1
			btn_receive.setTextColor(getResources().getColor(R.color.black));//2
			btn_repair.setTextColor(getResources().getColor(R.color.black));//3
			getTrackList(SettingLoader.getGuardNum(this), SettingLoader.getLoginUserType(this));
			break;
		default:
			break;
		}
	}
}
